package com.example.bottomnavigationdemo;

import androidx.lifecycle.ViewModelProvider;

import android.animation.ObjectAnimator;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class FirstFragment extends Fragment {

    private FirstViewModel mViewModel;
    private ImageView imageView;

    public static FirstFragment newInstance() {
        return new FirstFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.first_fragment, container, false);
        imageView = view.findViewById(R.id.imageView);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //设置为requireActivity，就是多页面切换回来，仍保持角度；this只负责当前页面
        mViewModel = new ViewModelProvider(requireActivity()).get(FirstViewModel.class);
        // TODO: Use the ViewModel

        imageView.setRotation(mViewModel.rotationPosition);
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "rotation", 0);
        objectAnimator.setDuration(500);//0.5s
        //点击图片触发事件
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!objectAnimator.isRunning()){
                    //连续点击时，若未执行完上一动作，就不再响应下一动作
                    objectAnimator.setFloatValues(imageView.getRotation(), imageView.getRotation()+100);
                    mViewModel.rotationPosition += 100;
                    objectAnimator.start();
                }

            }
        });
    }

}